#!/usr/bin/perl -w
################################################################################################################################
##
## Agente nagios para WebSphere Application Server
## Ruben Dario Garcia Alonso
## 2011/01/24   Version .9   Soporte para datos jvm, sesiones por contenedor war
##
################################################################################################################################

use LWP::Simple;
use XML::Simple;
use Data::Dumper;
 
 
my $tipo=$ARGV[0];
my $war=$ARGV[1];
my $crit=$ARGV[2];
my $tipo2=$ARGV[3];
my $puerto=$ARGV[4];
my $server=$ARGV[5];
my $tipo3=$ARGV[6];
my $tipo4=$ARGV[7];

sub getwasperf {
   my $url = shift;
   my $sysinfo = get $url;
   my $xml = new XML::Simple;
   $data = $xml->XMLin($sysinfo);
   return $data;
   #   return = $data->{'Node'}->{'Server'}->{'Stat'}->{'JVM Runtime'}->{'CountStatistic'}->{'UsedMemory'}->{'count'} );
}

################################################################################################################################
##
## Funcion de monitoreo de jvm de  websphere, incluye uso de jvm y de cpu
##
################################################################################################################################
sub checkjvm {
    my $war_val = shift;
	my $crit_val = shift;
    my $tipo2_val = shift;
	my $exit_code=0;
	my $msg="";
	if ($tipo2_val eq 'mem'){
		my $xmldata=getwasperf("http://$server:$puerto/wasPerfTool/servlet/perfservlet?Module=jvmRuntimeModule");
		my $jvmused =  sprintf("%d", $xmldata->{'Node'}->{'Server'}->{'Stat'}->{'CountStatistic'}->{'UsedMemory'}->{'count'} );
		my $jvmmax =  sprintf("%d", $xmldata->{'Node'}->{'Server'}->{'Stat'}->{'BoundedRangeStatistic'}->{'upperBound'} );
		my $values = int(($jvmused / $jvmmax) *100);
		if ( $values >  $crit_val ) {
			  $msg= "CRITICAL - JVM = $values; | JVM = $values;$war_val;$crit_val;;";
			  $exit_code=2;
		}
		elsif ($values > $war_val) {
			$msg= "WARNING - JVM = $values; | JVM = $values;$war_val;$crit_val;;";
			$exit_code=1;
		}
		else {
			$msg= "OK - JVM = $values; | JVM = $values;$war_val;$crit_val;;";
			$exit_code=0;
		}
	}
	elsif ($tipo2_val eq 'cpu'){
		my $xmldata=getwasperf("http://$server:$puerto/wasPerfTool/servlet/perfservlet?Module=jvmRuntimeModule");
		my $cpuused =  sprintf("%d", $xmldata->{'Node'}->{'Server'}->{'Stat'}->{'CountStatistic'}->{'ProcessCpuUsage'}->{'count'} );
		
		
		if ( $cpuused >  $crit_val ) {
			  $msg= "CRITICAL - CPU = $cpuused; | JVM = $cpuused;$war_val;$crit_val;;";
			  $exit_code=2;
		}
		elsif ($cpuused > $war_val) {
			$msg= "WARNING - CPU = $cpuused; | JVM = $cpuused;$war_val;$crit_val;;";
			$exit_code=1;
		}
		else {
			$msg= "OK - CPU = $cpuused; | JVM = $cpuused;$war_val;$crit_val;;";
			$exit_code=0;
		}
	
	}
	elsif ($tipo2_val eq 'gcinterval'){
		my $xmldata=getwasperf("http://$server:$puerto/wasPerfTool/servlet/perfservlet?Module=jvmRuntimeModule");
		my $gccount =  sprintf("%d", $xmldata->{'Node'}->{'Server'}->{'Stat'}->{'Stat'}->{'Garbage Collection'}->{'CountStatistic'}->{'count'} );
		my $gcinterval =  sprintf("%d", $xmldata->{'Node'}->{'Server'}->{'Stat'}->{'Stat'}->{'Garbage Collection'}->{'TimeStatistic'}->{'GCIntervalTime'}->{'totalTime'} );
		my $gctinterval = sprintf("%d",$gcinterval / $gccount);
		if ( $gctinterval >  $crit_val ) {
			  $msg= "CRITICAL - Garbage Collection Interval = $gctinterval milliseconds  response time; | Garbage Collection Interval = $gctinterval;$war_val;$crit_val;;";
			  $exit_code=2;
		}
		elsif ($gctinterval > $war_val) {
			$msg= "WARNING - Garbage Collection Interval = $gctinterval milliseconds response time; | Garbage Collection Interval = $gctinterval;$war_val;$crit_val;;";
			$exit_code=1;
		}
		else {
			$msg= "OK - Garbage Collection Interval = $gctinterval milliseconds response time  ;| Garbage Collection Interval = $gctinterval;$war_val;$crit_val;;";
			$exit_code=0;
		}
	
	}
	elsif ($tipo2_val eq 'gctime'){
		my $xmldata=getwasperf("http://$server:$puerto/wasPerfTool/servlet/perfservlet?Module=jvmRuntimeModule");
		my $gccount =  sprintf("%d", $xmldata->{'Node'}->{'Server'}->{'Stat'}->{'Stat'}->{'Garbage Collection'}->{'CountStatistic'}->{'count'} );
		my $gctime =  sprintf("%d", $xmldata->{'Node'}->{'Server'}->{'Stat'}->{'Stat'}->{'Garbage Collection'}->{'TimeStatistic'}->{'GCTime'}->{'totalTime'} );
		my $gcttime = sprintf("%d",$gctime / $gccount);
		if ( $gcttime >  $crit_val ) {
			  $msg= "CRITICAL - Garbage Collection Time = $gcttime; | Garbage Collection Time = $gcttime;$war_val;$crit_val;;";
			  $exit_code=2;
		}
		elsif ($gcttime > $war_val) {
			$msg= "WARNING - Garbage Collection Time = $gcttime; | Garbage Collection Time = $gcttime;$war_val;$crit_val;;";
			$exit_code=1;
		}
		else {
			$msg= "OK - Garbage Collection Time = $gcttime; | Garbage Collection Time = $gcttime;$war_val;$crit_val;;";
			$exit_code=0;
		}
	
	}
	return $msg,$exit_code;
}


################################################################################################################################
##
## Funcion de monitoreo de sesiones de  websphere por contenedor de servlet
##
################################################################################################################################
sub checksessions{
	my $war_val = shift;
	my $crit_val = shift;
	my $myear=shift;
	my $msg="";
	my $exit_code=0;
	my $xmldata=getwasperf("http://$server:$puerto/wasPerfTool/servlet/perfservlet?Module=servletSessionsModule");
	my $sesions =  sprintf("%d", $xmldata->{'Node'}->{'Server'}->{'Stat'}->{'Stat'}->{$myear}->{'RangeStatistic'}->{'LiveCount'}->{'value'} );		
	if ( $sesions >  $crit_val ) {
		  $msg= "CRITICAL - Sessions = $sesions; | Sessions = $sesions;$war_val;$crit_val;;";
		  $exit_code=2;
	}
	elsif ($sesions > $war_val) {
		$msg= "WARNING - Sessions = $sesions; | Sessions = $sesions;$war_val;$crit_val;;";
		$exit_code=1;
	}
	else {
		$msg= "OK - Sessions = $sesions; | Sessions = $sesions;$war_val;$crit_val;;";
		$exit_code=0;
	}
	return $msg,$exit_code;
}

################################################################################################################################
##
## Funcion de monitoreo de live transacctions de  websphere
##
################################################################################################################################
sub checktrans{
        my $war_val = shift;
        my $crit_val = shift;
        my $msg="";
        my $exit_code=0;
        my $xmldata=getwasperf("http://$server:$puerto/wasPerfTool/servlet/perfservlet?module=transactionModule");
        my $trans =  sprintf("%d", $xmldata->{'Node'}->{'Server'}->{'Stat'}->{'CountStatistic'}->{'ActiveCount'}->{'count'} );
        if ( $trans >  $crit_val ) {
                  $msg= "CRITICAL - Sessions = $trans; | Transacciones = $trans;$war_val;$crit_val;;";
                  $exit_code=2;
        }
        elsif ($trans > $war_val) {
                $msg= "WARNING - Sessions = $trans; | Transacciones = $trans;$war_val;$crit_val;;";
                $exit_code=1;
        }
        else {
                $msg= "OK - Sessions = $trans; | Transacciones = $trans;$war_val;$crit_val;;";
                $exit_code=0;
        }
        return $msg,$exit_code;
}


################################################################################################################################
##
## Funcion de monitoreo de threads de  websphere, incluye  numero  total de treadh y de threads en estado hang
##
################################################################################################################################

sub checkthreads {
    my $war_val = shift;
	my $crit_val = shift;
    my $tipo2_val = shift;
	my $msg="";
	my $exit_code=0;
	if ($tipo2_val eq 'num'){
		my $xmldata=getwasperf("http://$server:$puerto/wasPerfTool/servlet/perfservlet?module=threadPoolModule");
		my $numthreads =  sprintf("%d", $xmldata->{'Node'}->{'Server'}->{'Stat'}->{'Stat'}->{'WebContainer'}->{'BoundedRangeStatistic'}->{'value'} );
		if ( $numthreads >  $crit_val ) {
			  $msg= "CRITICAL - Threads = $numthreads; | Threads = $numthreads;$war_val;$crit_val;;";
			  $exit_code=2;
		}
		elsif ($numthreads > $war_val) {
			$msg= "WARNING - Threads = $numthreads; | Threads = $numthreads;$war_val;$crit_val;;";
			$exit_code=1;
		}
		else {
			$msg= "OK - Threads = $numthreads; | Threads = $numthreads;$war_val;$crit_val;;";
			$exit_code=0;
		}
	}
	elsif ($tipo2_val eq 'hangs') {
		my $xmldata=getwasperf("http://$server:$puerto/wasPerfTool/servlet/perfservlet?module=threadPoolModule");
		my $numhangs =  sprintf("%d", $xmldata-> {'Node'}->{'Server'}->{'Stat'}->{'Stat'}->{'WebContainer'}->{'RangeStatistic'}->{'value'} );
		if ( $numhangs >  $crit_val ) {
			  $msg= "CRITICAL - Hangs = $numhangs; | Hangs = $numhangs;$war_val;$crit_val;;";
			  $exit_code=2;
		}
		elsif ($numhangs > $war_val) {
			$msg= "WARNING - Hangs = $numhangs; | Hangs = $numhangs;$war_val;$crit_val;;";
			$exit_code=1;
		}
		else {
			$msg= "OK - Hangs = $numhangs; | Hangs = $numhangs;$war_val;$crit_val;;";
			$exit_code=0;
		}
	}
	
	return $msg,$exit_code;
}
 

################################################################################################################################
##
## Funcion de monitoreo de jdbcs de  websphere, incluye funciones de  numero de pools por datasource,  
#  wait y used time por  datasource
##
################################################################################################################################

sub checkjdbc {
	my $war_val = shift;
	my $crit_val = shift;
    my $tipo2_val = shift;
	my $jdbcname = shift;
	my $dataname = shift;
	my $msg="";
	my $exit_code=0;
	if ($tipo2_val eq 'conections'){
		my $xmldata=getwasperf("http://$server:$puerto/wasPerfTool/servlet/perfservlet?module=connectionPoolModule");
		my $numcons =  sprintf("%d", $xmldata->{'Node'}->{'Server'}->{'Stat'}->{'Stat'}->{$jdbcname}->{'Stat'}->{$dataname}->{'BoundedRangeStatistic'}->{'PoolSize'}->{'value'} );
		if ( $numcons >  $crit_val ) {
			  $msg= "CRITICAL - Conections = $numcons; | Conections = $numcons;$war_val;$crit_val;;";
			  $exit_code=2;
		}
		elsif ($numcons > $war_val) {
			$msg= "WARNING - Conections = $numcons; | Conections = $numcons;$war_val;$crit_val;;";
			$exit_code=1;
		}
		else {
			$msg= "OK - Conections = $numcons; | Conections = $numcons;$war_val;$crit_val;;";
			$exit_code=0;
		}
	}
	elsif ($tipo2_val eq 'waitt') {
		my $xmldata=getwasperf("http://$server:$puerto/wasPerfTool/servlet/perfservlet?module=connectionPoolModule");
		my $waittime =  sprintf("%d", $xmldata-> {'Node'}->{'Server'}->{'Stat'}->{'Stat'}->{$jdbcname}->{'Stat'}->{$dataname}->{'TimeStatistic'}->{'WaitTime'}->{'totalTime'});
		if ( $waittime >  $crit_val ) {
			  $msg= "CRITICAL - Wait_time = $waittime; | Wait_time = $waittime;$war_val;$crit_val;;";
			  $exit_code=2;
		}
		elsif ($waittime > $war_val) {
			$msg= "WARNING - Wait_time = $waittime; | Wait_time = $waittime;$war_val;$crit_val;;";
			$exit_code=1;
		}
		else {
			$msg= "OK - Wait_time = $waittime; | Wait_time = $waittime;$war_val;$crit_val;;";
			$exit_code=0;
		}
	}
	elsif ($tipo2_val eq 'used') {
		my $xmldata=getwasperf("http://$server:$puerto/wasPerfTool/servlet/perfservlet?module=connectionPoolModule");
		my $usetime =  sprintf("%d", $xmldata-> {'Node'}->{'Server'}->{'Stat'}->{'Stat'}->{$jdbcname}->{'Stat'}->{$dataname}->{'TimeStatistic'}->{'UseTime'}->{'totalTime'});
		if ( $usetime >  $crit_val ) {
			  $msg= "CRITICAL - Used_time = $usetime; | Used_time = $usetime;$war_val;$crit_val;;";
			  $exit_code=2;
		}
		elsif ($usetime > $war_val) {
			$msg= "WARNING - Used_time = $usetime; | Used_time = $usetime;$war_val;$crit_val;;";
			$exit_code=1;
		}
		else {
			$msg= "OK - Used_time = $usetime; | Used_time = $usetime;$war_val;$crit_val;;";
			$exit_code=0;
		}
	}
	
	return $msg,$exit_code;
}

################################################################################################################################
##
## Funcion de monitoreo de servlets
##
################################################################################################################################

sub checkservlets {
        my $war_val = shift;
        my $crit_val = shift;
        my $tipo2_val = shift;
        my $warapp = shift;
        my $servlet = shift;
        my $msg="";
        my $exit_code=0;
        if ($tipo2_val eq 'warapp'){
                my $xmldata=getwasperf("http://$server:$puerto/wasPerfTool/servlet/perfservlet?module=webAppModule");
                my $countservlet =  sprintf("%d", $xmldata->{'Node'}->{'Server'}->{'Stat'}->{'Stat'}->{$warapp}->{'Stat'}->{'CountStatistic'}->{'RequestCount'}->{'count'} );
                my $timeservlet =  sprintf("%d", $xmldata->{'Node'}->{'Server'}->{'Stat'}->{'Stat'}->{$warapp}->{'Stat'}->{'TimeStatistic'}->{'ServiceTime'}->{'totalTime'} );
                my $servicetime= sprintf("%d", $timeservlet/$countservlet);
                if ( $servicetime >  $crit_val ) {
                          $msg= "CRITICAL - Service Time = $servicetime; | Service Time = $servicetime;$war_val;$crit_val;;";
                          $exit_code=2;
                }
                elsif ($servicetime > $war_val) {
                        $msg= "WARNING - Service Time = $servicetime; | Service Time = $servicetime;$war_val;$crit_val;;";
                        $exit_code=1;
                }
                else {
                        $msg= "OK - Service Time = $servicetime; | Service Time = $servicetime;$war_val;$crit_val;;";
                        $exit_code=0;
                }
        }
        elsif ($tipo2_val eq 'servlet') {
                my $xmldata=getwasperf("http://$server:$puerto/wasPerfTool/servlet/perfservlet?module=webAppModule");
                my $countservlet =  sprintf("%d", $xmldata->{'Node'}->{'Server'}->{'Stat'}->{'Stat'}->{$warapp}->{'Stat'}->{'Stat'}->{$servlet}->{'Stat'}->{'Stat'}->{'CountStatistic'}->{'count'} );
                my $timeservlet =  sprintf("%d", $xmldata->{'Node'}->{'Server'}->{'Stat'}->{'Stat'}->{$warapp}->{'Stat'}->{'Stat'}->{$servlet}->{'Stat'}->{'Stat'}->{'TimeStatistic'}->{'totalTime'} );
                my $servicetime= sprintf("%d",$timeservlet/$countservlet);
                if ( $servicetime >  $crit_val ) {
                          $msg= "CRITICAL - Servlet Time = $servicetime; |  Servlet Time = $servicetime;$war_val;$crit_val;;";
                          $exit_code=2;
                }
                elsif ($servicetime > $war_val) {
                        $msg= "WARNING - Servlet Time = $servicetime; |  Servlet Time = $servicetime;$war_val;$crit_val;;";
                        $exit_code=1;
                }
                else {
                        $msg= "OK - Servlet Time = $servicetime; |  Servlet Time = $servicetime;$war_val;$crit_val;;";
                        $exit_code=0;
                }
        }

        return $msg,$exit_code;
}

 
################################################################################################################################
##
## comando check_was
##
################################################################################################################################

 if ($tipo eq 'jvm') {
	my ($mensaje,$exit_code) = checkjvm($war,$crit,$tipo2);
	print "$mensaje\n";
	exit $exit_code;
}
elsif ($tipo eq 'sessions') {
	my ($mensaje,$exit_code)=checksessions($war,$crit,$tipo2);
	print "$mensaje\n";
	exit $exit_code;
}
elsif ($tipo eq 'threads') {
	my ($mensaje,$exit_code)=checkthreads($war,$crit,$tipo2);
	print "$mensaje\n";
	exit $exit_code;
}
elsif ($tipo eq 'jdbc') {
	my ($mensaje,$exit_code)=checkjdbc($war,$crit,$tipo2,$tipo3,$tipo4);
	print "$mensaje\n";  
	exit $exit_code;
}
elsif ($tipo eq 'trans') {
        my ($mensaje,$exit_code)=checktrans($war,$crit);
        print "$mensaje\n";
        exit $exit_code;
}
elsif ($tipo eq 'servlet') {
        my ($mensaje,$exit_code)=checkservlets($war,$crit,$tipo2,$tipo3,$tipo4);
        print "$mensaje\n";
        exit $exit_code;
}
else {
	print "Los parametros  estan equivocados";
	exit 3;
}
